1 /*
2  * Copyright (c) 2013 - Andre Roth <neolynx@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation version 2.1 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17  *
18  */
19 
20 /**
21  * @file atsc_eit.h
22  * @ingroup dvb_table
23  * @brief Provides the table parser for the ATSC EIT (Event Information Table)
24  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
25  * @author Mauro Carvalho Chehab
26  * @author Andre Roth
27  *
28  * @par Relevant specs
29  * The table described herein is defined at:
30  * - ATSC A/65:2009
31  *
32  * @see
33  * http://www.etherguidesystems.com/help/sdos/atsc/syntax/tablesections/eitks.aspx
34  *
35  * @par Bug Report
36  * Please submit bug reports and patches to linux-media@vger.kernel.org
37  */
38 
39 module libdvbv5_d.atsc_eit;
40 
41 import core.stdc.time;
42 import core.sys.posix.unistd;
43 
44 import libdvbv5_d.descriptors: dvb_desc;
45 import libdvbv5_d.dvb_fe: dvb_v5_fe_parms;
46 import libdvbv5_d.header: dvb_table_header;
47 
48 extern (C):
49 
50 /* ssize_t */
51 
52 /**
53  * @def ATSC_TABLE_EIT
54  *	@brief ATSC EIT table ID
55  *	@ingroup dvb_table
56  */
57 enum ATSC_TABLE_EIT = 0xCB;
58 
59 /**
60  * @struct atsc_table_eit_event
61  * @brief ATSC EIT event table
62  * @ingroup dvb_table
63  *
64  * @param event_id	an uniquelly (inside a service ID) event ID
65  * @param title_length	title length. Zero means no title
66  * @param duration	duration in seconds
67  * @param etm		Extended Text Message location
68  * @param descriptor	pointer to struct dvb_desc
69  * @param next		pointer to struct atsc_table_eit_event
70  * @param start		event start (in struct tm format)
71  * @param source_id	source id (obtained from ATSC header)
72  *
73  * This structure is used to store the original ATSC EIT event table,
74  * converting the integer fields to the CPU endianness, and converting the
75  * timestamps to a way that it is better handled on Linux.
76  *
77  * The undocumented parameters are used only internally by the API and/or
78  * are fields that are reserved. They shouldn't be used, as they may change
79  * on future API releases.
80  *
81  * Everything after atsc_table_eit_event::descriptor (including it) won't
82  * be bit-mapped to the data parsed from the MPEG TS. So, metadata are added
83  * there.
84  */
85 struct atsc_table_eit_event
86 {
87     align (1):
88 
89     union
90     {
91         align (1):
92 
93         ushort bitfield;
94 
95         struct
96         {
97             import std.bitmanip : bitfields;
98             align (1):
99 
100             mixin(bitfields!(
101                 ushort, "event_id", 14,
102                 ushort, "one", 2));
103         }
104     }
105 
106     uint start_time;
107 
108     union
109     {
110         align (1):
111 
112         uint bitfield2;
113 
114         struct
115         {
116             import std.bitmanip : bitfields;
117             align (1):
118 
119             mixin(bitfields!(
120                 uint, "title_length", 8,
121                 uint, "duration", 20,
122                 uint, "etm", 2,
123                 uint, "one2", 2,
124                 uint, "", 2,
125                 uint, "", 30));
126         }
127     }
128 
129     // struct dvb_desc;
130     dvb_desc* descriptor;
131     atsc_table_eit_event* next;
132     tm start;
133     ushort source_id;
134 }
135 
136 /**
137  * @union atsc_table_eit_desc_length
138  * @brief ATSC EIT descriptor length
139  * @ingroup dvb_table
140  *
141  * @param desc_length	descriptor length
142  *
143  * This structure is used to store the original ATSC EIT event table,
144  * converting the integer fields to the CPU endianness, and converting the
145  * timestamps to a way that it is better handled on Linux.
146  *
147  * The undocumented parameters are used only internally by the API and/or
148  * are fields that are reserved. They shouldn't be used, as they may change
149  * on future API releases.
150  */
151 union atsc_table_eit_desc_length
152 {
153     align (1):
154 
155     ushort bitfield;
156 
157     struct
158     {
159         import std.bitmanip : bitfields;
160         align (1):
161 
162         mixin(bitfields!(
163             ushort, "desc_length", 12,
164             ushort, "reserved", 4));
165     }
166 }
167 
168 /**
169  * @struct atsc_table_eit
170  * @brief ATSC EIT table
171  * @ingroup dvb_table
172  *
173  * @param header			struct dvb_table_header content
174  * @param protocol_version		protocol version
175  * @param events			events
176  * @param event				pointer to struct atsc_table_eit_event
177  *
178  * This structure is used to store the original ATSC EIT table,
179  * converting the integer fields to the CPU endianness.
180  *
181  * Everything after atsc_table_eit::event (including it) won't
182  * be bit-mapped to the data parsed from the MPEG TS. So, metadata are added
183  * there.
184  */
185 struct atsc_table_eit
186 {
187     align (1):
188 
189     dvb_table_header header;
190     ubyte protocol_version;
191     ubyte events;
192     atsc_table_eit_event* event;
193 }
194 
195 /**
196  * @brief Macro used to find event on an ATSC EIT table
197  * @ingroup dvb_table
198  *
199  * @param _event			event to seek
200  * @param _eit				pointer to struct atsc_table_eit_event
201  */
202 
203 // struct dvb_v5_fe_parms;
204 
205 /**
206  * @brief Initializes and parses ATSC EIT table
207  * @ingroup dvb_table
208  *
209  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
210  * @param buf buffer containing the EIT raw data
211  * @param buflen length of the buffer
212  * @param table pointer to struct atsc_table_eit to be allocated and filled
213  *
214  * This function allocates an ATSC EIT table and fills the fields inside
215  * the struct. It also makes sure that all fields will follow the CPU
216  * endianness. Due to that, the content of the buffer may change.
217  *
218  * @return On success, it returns the size of the allocated struct.
219  *	   A negative value indicates an error.
220  */
221 ssize_t atsc_table_eit_init (
222     dvb_v5_fe_parms* parms,
223     const(ubyte)* buf,
224     ssize_t buflen,
225     atsc_table_eit** table);
226 
227 /**
228  * @brief Frees all data allocated by the ATSC EIT table parser
229  * @ingroup dvb_table
230  *
231  * @param table pointer to struct atsc_table_eit to be freed
232  */
233 void atsc_table_eit_free (atsc_table_eit* table);
234 
235 /**
236  * @brief Prints the content of the ATSC EIT table
237  * @ingroup dvb_table
238  *
239  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
240  * @param table pointer to struct atsc_table_eit
241  */
242 void atsc_table_eit_print (dvb_v5_fe_parms* parms, atsc_table_eit* table);
243 
244 /**
245  * @brief Converts an ATSC EIT formatted timestamp into struct tm
246  * @ingroup ancillary
247  *
248  * @param start_time	event on ATSC EIT time format
249  * @param tm		pointer to struct tm where the converted timestamp will
250  *			be stored.
251  */
252 void atsc_time (const uint start_time, tm* tm);
253